package com.flowable.test;

import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : liYang
 * @Version: 1.0
 * @date : 2024/11/21 17:57
 * @Desc : 增加部署流程定义
 */
public class FlowableTest02 {

    /*
     * 部署流程
     * 参考 flowable 6.5 的中文手册
     * */

    @Test
    public void testDeploy(){
        // 配置数据库相关信息 获取 ProcessEngineConfiguration
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://8.130.118.29:3306/flowabledb")
                .setJdbcUsername("flowable_uesr")
                .setJdbcPassword("flowable@2024")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 1、获取 ProcessEngine 对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        // 2、部署获取 RepositoryService 对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()// 创建Deployment对象
                .addClasspathResource("holiday-request.bpmn20.xml") // 添加流程部署文件
                .name("请求流程") // 设置部署流程的名称
                .deploy();
        System.out.println("deployment.getId() = " + deployment.getId());
        System.out.println("deployment.getName() = " + deployment.getName());
    }
    /* deployment.getId() = 2501
     * deployment.getName() = 请求流程*/
    /* deployment.getId() = 7501
     * deployment.getName() = 请求流程*/
    /*deployment.getId() = 17501
     *deployment.getName() = 请求流程*/

    /*
     * 查询流程
     * 参考 flowable 6.5 的中文手册
     * */

    @Test
    public void testDeployQuery(){
        // 配置数据库相关信息 获取 ProcessEngineConfiguration
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://8.130.118.29:3306/flowabledb")
                .setJdbcUsername("flowable_uesr")
                .setJdbcPassword("flowable@2024")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 1、获取 ProcessEngine 对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        // 2、部署获取 RepositoryService 对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 3、获取流程定义对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId("17501")//多个是Ids
                .singleResult();
        // 打印获取相关信息
        System.out.println("processDefinition.getId() = " + processDefinition.getId());
        System.out.println("processDefinition.getName() = " + processDefinition.getName());
        System.out.println("processDefinition.getDeploymentId() = " + processDefinition.getDeploymentId());
        System.out.println("processDefinition.getDescription() = " + processDefinition.getDescription());
    }
    /* 查询结果
    processDefinition.getId() = holidayRequest:1:3
    processDefinition.getName() = Holiday Request
    processDefinition.getDeploymentId() = 1
    processDefinition.getDescription() = null
    * */
    /*
    processDefinition.getId() = holidayRequest:1:17503
    processDefinition.getName() = 请假流程
    processDefinition.getDeploymentId() = 17501
    processDefinition.getDescription() = null
    * */

    /*
    * 删除流程
    * */
    @Test
    public void testDeployDelete(){
        // 配置数据库相关信息 获取 ProcessEngineConfiguration
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://8.130.118.29:3306/flowabledb")
                .setJdbcUsername("flowable_uesr")
                .setJdbcPassword("flowable@2024")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 1、获取 ProcessEngine 对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        // 2、部署获取 RepositoryService 对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 3、删除部署流程ID，如果部署流程启动了就不允许删除了
        /*repositoryService.deleteDeployment("1");*/
        // 第二个参数是级联删除，如果流程启动了一并删除，相关任务被删除。
        repositoryService.deleteDeployment("12501",true);
    }

    /*
    * 启动流程实例
    * -- 在已经在流程引擎中部署了流程定义，因此可以使用这个流程定义作为“蓝图”启动流程实例
    * */

    @Test
    public void testRunProcess(){
        // 配置数据库相关信息 获取 ProcessEngineConfiguration
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://8.130.118.29:3306/flowabledb")
                .setJdbcUsername("flowable_uesr")
                .setJdbcPassword("flowable@2024")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 1、获取 ProcessEngine 对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        // 2、启动流程实例通过 RuntimeService 对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 3、构建流程变量
        Map<String,Object> variables = new HashMap<>();
        // 添加参数绑定流程
        variables.put("employee","小明") ;// 谁申请请假
        variables.put("nrOfHolidays",3); // 请几天假
        variables.put("description","休假写工作流"); // 请假的原因
        // 启动流程实例，第一个参数是流程定义的id
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey("holidayRequest", variables);// 启动流程实例
        // 输出相关的流程实例信息
        System.out.println("流程定义的ID：" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例的ID：" + processInstance.getId());
        System.out.println("当前活动的ID：" + processInstance.getActivityId());
    }
    /*
    * 流程定义的ID：holidayRequest:1:2503
    * 流程实例的ID：5001
    * 当前活动的ID：null
    * */

    /*流程定义的ID：holidayRequest:1:7503
    * 流程实例的ID：10001
    * 当前活动的ID：null*/

    /*流程定义的ID：holidayRequest:1:17503
    * 流程实例的ID：20001
    * 当前活动的ID：null
    * */

    /*
    * 查询流程任务查询
    * */
    @Test
    public void testQueryTask(){
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://8.130.118.29:3306/flowabledb")
                .setJdbcUsername("flowable_uesr")
                .setJdbcPassword("flowable@2024")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 1、获取 ProcessEngine 对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        // 2、任务相关查询 TaskService
        TaskService taskService = processEngine.getTaskService();
        // 3、获取查询对象
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("holidayRequest")
                .taskAssignee("Boos")
                .list();
        for (Task task : list) {
            System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
            System.out.println("task.getName() = " + task.getName());
        }
    }
    /* *查询任务结果
    task.getProcessDefinitionId() = holidayRequest:1:17503
    task.getId() = 20009
    task.getAssignee() = Boos
    task.getName() = 同意或拒绝请假
    -- 到达排他网关进行处理，选择通过与欲绝
    * */

    /*
    * 处理任务操作，完成流程任中当前任务
    * */
    @Test
    public void testCompleteTask(){
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://8.130.118.29:3306/flowabledb")
                .setJdbcUsername("flowable_uesr")
                .setJdbcPassword("flowable@2024")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 1、获取 ProcessEngine 对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        // 2、任务相关查询 TaskService
        TaskService taskService = processEngine.getTaskService();
        // 3、获取查询对象
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("holidayRequest")
                .taskAssignee("Boos")
                .singleResult();
        // 4、添加流程变量
        Map<String,Object> variables = new HashMap<>();
        variables.put("approved",false); // 拒绝请假
        //5、完成任务
        taskService.complete(task.getId(),variables);
    }
    /*
    * 13:29:12.870 [main] DEBUG org.flowable.engine.impl.agenda.ContinueProcessOperation - Executing activityBehavior class org.flowable.engine.impl.bpmn.helper.ClassDelegate on activity 'sendRejectionMail' with execution 20005
      不好意思，你的请假申请被拒绝了.......*/

    /*
    * 历史任务查询
    * */
    @Test
    public void testQueryHistory(){
        ProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://8.130.118.29:3306/flowabledb")
                .setJdbcUsername("flowable_uesr")
                .setJdbcPassword("flowable@2024")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 1、获取 ProcessEngine 对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        // 2、获取 HistoryService
        HistoryService historyService = processEngine.getHistoryService();
        // 3、获取查询对象
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()// 定义查询条件 holidayRequest:1:17503
                .processDefinitionId("holidayRequest:1:17503")
                .finished() //查询历史记录的状态是已经完成的
                .orderByHistoricActivityInstanceEndTime().asc()// 指定排序字段和顺序
                .list();
        for (HistoricActivityInstance hsitory : list) {
            //遍历历史记录
            System.out.println( hsitory.getActivityId() + " 工作流: " + hsitory.getActivityName() +"--"+ hsitory.getAssignee()+"--"
                    + hsitory.getDurationInMillis() + " 毫秒");
        }

    }
}













